package com.bxd.baodan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.BaseActivity;
import com.bxd.baodan.activity.WebViewPager;
import com.bxd.baodan.item.SchemeBean.DataBean.SchemeBean_;
import com.bxd.baodan.util.GlideUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * Created by lizhichao on 17/7/24.
 */


public class SchemeAdapter extends RecyclerView.Adapter<SchemeAdapter.ViewHolder> {
    private Context context;
    private List<SchemeBean_> mData;

    public SchemeAdapter(List<SchemeBean_> data, Context context) {
        this.mData = data;
        this.context = context;
    }

    public void updateData(List<SchemeBean_> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.scheme_home_item, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final SchemeBean_ item = mData.get(position);

        GlideUtil.loadImage(context, item.image_s, holder.iv);
        holder.title.setText(item.title);
        holder.description.setText(item.description);
        holder.main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(App.getContext(), "v6_0_0_SY_faxq");
                //活动测试
                 item.url = "http://ecuat.taikang.com/channel/coop/page/bxd/page/details.html";
                  ((BaseActivity) context).openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{item.url});
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout main;
        private ImageView iv;
        private TextView title;
        private TextView description;


        public ViewHolder(View view) {
            super(view);
            main = (LinearLayout) view.findViewById(R.id.main);
            iv = (ImageView) view.findViewById(R.id.iv);
            title = (TextView) view.findViewById(R.id.title);
            description = (TextView) view.findViewById(R.id.description);
        }
    }
}

